package com.bw.module_mine.ui.login

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_common.base.BaseFragment
import com.bw.lib_common.base.state.RequestEvent
import com.bw.lib_common.utils.ToastUtils
import com.bw.lib_common.utils.ValidUtil
import com.bw.module_mine.R
import com.bw.module_mine.databinding.FragmentResetPwdBinding
import com.bw.module_mine.ui.login.viewmodel.LoginViewModel
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch


class ResetPwdFragment : BaseFragment<LoginViewModel, FragmentResetPwdBinding>() {
    private val mViewModel: LoginViewModel by  activityViewModels()
    override val mLayoutResId: Int
        get() = R.layout.fragment_reset_pwd
    private lateinit var mNavController: NavController

    override fun initView() {
        super.initView()
        //设置当前正在显示的fragment
        mViewModel.setShowingFragmentTag(AccountLoginFragment::class.java.simpleName)
        mNavController = Navigation.findNavController(mBinding.root)

    }

    override fun initData() {
        super.initData()
        mBinding.viewModel = mViewModel
    }

    override fun initListener() {
        super.initListener()
        mBinding.btnLogin.setOnClickListener {
            checkPhone()
        }
        lifecycleScope.launch {
            mViewModel.requestResetEvent_.collect {
                when (it) {
                    is RequestEvent.Success -> {
                        setLoadingViewVisible(false)
                        //跳转到登录成功页面
                        (requireActivity() as LoginActivity).handleLoginSuccess()

                    }
                    is RequestEvent.Error -> {
                        setLoadingViewVisible(false)
                        ToastUtils.showShort(requireActivity(),it.msg)
                    }
                    is RequestEvent.Loading -> {
                        setLoadingViewVisible( true)
                    }
                    else -> {}
                }
            }
        }
        mBinding.etSecPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                //校验输入的密码是否包含至少包含两种字符
                mBinding.restPsdMatched = mBinding.etPassword.text.toString().equals(mBinding.etSecPassword.text.toString())
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })


    }

    private fun checkPhone() {
        val restPassword = mViewModel.restPassword.value
        val password = mViewModel.confirmPassword.value
//        if (!mLoginAgreement) {
//            (activity as BaseActivity<*, *>?)?.dismissKeyboard()
//            ToastUtils.showShort(context, "请先勾选同意《用户协议》和《隐私政策》")
////            mPopupAgreement.show(mBinding.btnCheck)
//            return
//        }

//        if (!ValidUtil.phoneNumberValid(phone)) {
//            ToastUtils.showShort(context, "手机号码长度错误")
//            return
//        }
        //验证密码不为空
        if (password.isNullOrEmpty()){
            ToastUtils.showShort(context, "请输入密码")
            return
        }
        if (restPassword.isNullOrEmpty()){
            ToastUtils.showShort(context, "请输入二次密码")
            return
        }
        if (restPassword != password){
            ToastUtils.showShort(context, "两次密码不一致")
            return
        }
        mViewModel.newPwd()
    }
}